package com.springboot.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Author: TongRui乀
 * @Date: 2019/10/16 13:35
 * @description：  主数据源配置
 */
@Configuration
@MapperScan(basePackages = "com.springboot.mapper.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class DataSourcePrimary {

    @Autowired
    private Environment environment;

    @Bean("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    @Primary
    public DataSource primaryDataSource(){

        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setUrl(environment.getProperty("spring.datasource.primary.url"));
        dataSource.setUsername(environment.getProperty("spring.datasource.primary.username"));
        dataSource.setPassword(environment.getProperty("spring.datasource.primary.password"));
        dataSource.setDriverClassName(environment.getProperty("spring.datasource.primary.driver-class-name"));

        return dataSource;
    }

    @Bean("primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {

        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

        factoryBean.setDataSource(primaryDataSource);

        // 设置mapping xml文件位置
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/springboot/mapping/primary/*.xml"));

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();

        // 设置驼峰映射
        configuration.setMapUnderscoreToCamelCase(true);

        factoryBean.setConfiguration(configuration);

        return factoryBean.getObject();
    }


    @Bean("primaryDataSourceTransactionManager")
    @Primary
    public DataSourceTransactionManager primaryDataSourceTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource){
        return new DataSourceTransactionManager(primaryDataSource);
    }

    @Bean("primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory primarySqlSessionFactory){
        return new SqlSessionTemplate(primarySqlSessionFactory);
    }

}
